﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Quellenverwaltung.Data.Source
{
    public class Person
    {
        public Person()
        {
        }

        public Person(String name)
        {
            PersonAsString = name;
        }

        [XmlElement(ElementName="First", Namespace="http://schemas.microsoft.com/office/word/2004/10/bibliography")]
        public String First { get; set; }
        [XmlElement(ElementName="Last", Namespace = "http://schemas.microsoft.com/office/word/2004/10/bibliography")]
        public String Last { get; set; }

        [XmlIgnore]
        public String PersonAsString
        {
            get
            {
                if (First != null && First.Length > 0)
                {
                    return Last + ", " + First;
                }
                else
                {
                    return Last;
                }
            }
            set
            {
                if (value.Contains(","))
                {
                    String[] names = value.Split(',');
                    if (names.Count() > 1)
                    {
                        Last = names[0].Trim();
                        First = names[1].Trim();
                    }
                }
                else if (value.Contains(' '))
                {
                        int index = value.LastIndexOf(' ');

                        First = value.Substring(0, index).Trim();
                        Last = value.Substring(index + 1).Trim();
                }
                else
                {
                    Last = value;
                }
                
            }
        }
    }
}
